package meihua.stopcarapp.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import meihua.stopcarapp.R;
import meihua.stopcarapp.app.UserConfig;
import meihua.stopcarapp.utils.ToastUtil;

/**
 * Name: LGZ
 * Time: 2017/5/17.
 * Dec:
 */

public class UserInfoSexDialog extends Dialog {

    private Context context;
    private OnSelectSexClickListener onSelectSexClickListener;
    String SEX_MAIN = "男";
    String SEX_WOMAIN = "女";
    private String sex = UserConfig.USER_ENTITY.getUser_sex();

    public UserInfoSexDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public UserInfoSexDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    protected UserInfoSexDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.context = context;
    }

    public OnSelectSexClickListener getOnSelectSexClickListener() {
        return onSelectSexClickListener;
    }

    public void setOnSelectSexClickListener(OnSelectSexClickListener onSelectSexClickListener) {
        this.onSelectSexClickListener = onSelectSexClickListener;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private TextView btnSubmit,btnCancle;
    private RadioGroup radioGroup;
    private RadioButton rb_man,rb_woman;
    private void init() {
        //以view的方式引入，然后回调activity方法，setContentView，实现自定义布局
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_userinfo_sex, null);
        setContentView(view);
        //点击屏幕其它地方，dialog是否消失
//        setCancelable(false);
        btnCancle = (TextView) view.findViewById(R.id.dialog_userinfo_sex_can);
        btnSubmit = (TextView) view.findViewById(R.id.dialog_userinfo_sex_sub);
        radioGroup = (RadioGroup) view.findViewById(R.id.dialog_userinfo_sex_rg);
        rb_man = (RadioButton) view.findViewById(R.id.dialog_userinfo_sex_rb_m);
        rb_woman = (RadioButton) view.findViewById(R.id.dialog_userinfo_sex_rb_g);
        if (sex.equals(SEX_MAIN)) rb_man.setChecked(true);
        else rb_woman.setChecked(true);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (R.id.dialog_userinfo_sex_rb_m == checkedId) sex = SEX_MAIN;
                if (R.id.dialog_userinfo_sex_rb_g == checkedId) sex = SEX_WOMAIN;
                ToastUtil.showShort(context,sex);
            }
        });
        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectSexClickListener.cancleClick();
//                this.dismiss();
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectSexClickListener.submitClick();
            }
        });
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.7); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public interface OnSelectSexClickListener{
        void submitClick();
        void cancleClick();
    }


}
